package com.example.wuqujingdianphone.fragment.mainfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnItemClickListener;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.BrowsingHistoryActivity;
import com.example.wuqujingdianphone.activity.LoginActivity;
import com.example.wuqujingdianphone.activity.MessageModifyActivity;
import com.example.wuqujingdianphone.activity.PassWordModifyActivity;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.utils.Message1Event;
import com.example.wuqujingdianphone.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by yuanpk on 2018/4/9.
 */

public class MyFragment extends Fragment {
    private static final String TAG = "MyFragment";
    @BindView(R.id.tv_username)
    TextView mTvUsername;
    @BindView(R.id.tv_grade)
    TextView mTvGrade;
    @BindView(R.id.tv_browsing_history)
    TextView mTvBrowsingHistory;
    @BindView(R.id.tv_message_modify)
    TextView mTvMessageModify;
    @BindView(R.id.tv_password_modify)
    TextView mTvPasswordModify;
    @BindView(R.id.tv_login_out)
    TextView mTvLoginOut;
    Unbinder unbinder;
    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.huiyuan_layout)
    LinearLayout huiyuanLayout;
    @BindView(R.id.login_in_layout)
    LinearLayout loginInLayout;
    @BindView(R.id.login_out_layout)
    LinearLayout loginOutLayout;

    public static MyFragment getMyFragment() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
//        TextView textView = (TextView) getActivity().findViewById(R.id.title);
        title.setText("我的");
        initView();
        return view;
    }

    private void initView() {
        if (SPUtil.getBoolean(getActivity(), "isLogin", false)) {
            huiyuanLayout.setVisibility(View.VISIBLE);
            String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
            mTvUsername.setText(account + " 您好");

            loginInLayout.setVisibility(View.GONE);
            loginOutLayout.setVisibility(View.VISIBLE);

        } else {
            huiyuanLayout.setVisibility(View.GONE);
            String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
            mTvUsername.setText(account + " 您好");

            loginInLayout.setVisibility(View.VISIBLE);
            loginOutLayout.setVisibility(View.GONE);
        }

        mTvBrowsingHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!SPUtil.getBoolean(getActivity(), "isLogin", false)) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), BrowsingHistoryActivity.class));
                }
            }
        });

        mTvMessageModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MessageModifyActivity.class));
            }
        });


        mTvPasswordModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), PassWordModifyActivity.class));
            }
        });
    }

    @OnClick({R.id.login_in_layout, R.id.login_out_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_in_layout:
                startActivity(new Intent(getActivity(), LoginActivity.class));
                break;
            case R.id.login_out_layout:
                alertShow();
                break;
        }
    }

    public void alertShow() {
       AlertView alertView = new AlertView("提示", "您确定要退出登录吗？", null, new String[]{"确定", "取消"}, null, getActivity(), AlertView.Style.Alert, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                if (position == 0) {//确定
                    SPUtil.putBoolean(getActivity(), "isLogin", false);

                    huiyuanLayout.setVisibility(View.GONE);
//                    SPUtil.putString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
                    String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
                    mTvUsername.setText(account + " 您好");

                    loginInLayout.setVisibility(View.VISIBLE);
                    loginOutLayout.setVisibility(View.GONE);
                    EventBus.getDefault().post(new Message1Event(3,GlobalConfig.LOG_OUT));
                } else if (position == 1) {//取消

                }

            }
        });
       alertView.show();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsgEvent(Message1Event messageEvent) {
        String msg = messageEvent.getMsg();
        if (msg.equals(GlobalConfig.LOGIN_IS_OK)) {
            huiyuanLayout.setVisibility(View.VISIBLE);
            String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
            mTvUsername.setText(account + " 您好");

            loginInLayout.setVisibility(View.GONE);
            loginOutLayout.setVisibility(View.VISIBLE);
        }

        if (msg.equals(GlobalConfig.LOG_OUT)) {
            huiyuanLayout.setVisibility(View.GONE);
            String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
            mTvUsername.setText(account + " 您好");

            loginInLayout.setVisibility(View.VISIBLE);
            loginOutLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }
}
